home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / tchrt3.zip / WATCH.C < prev    next >
Text File  |  1990-07-18  |  18KB  |  437 lines

  1. /*---------------------------------------------------------------------------
  2.  |  Program WATCH                                                           |
  3.  |                                                                          |
  4.  |  This program creates a stopwatch on the display with "Taylor" split     |
  5.  |  capability.                                                             |
  6.  |                                                                          |
  7.  |  This program uses two timers to keep track of total and lap times.      |
  8.  |  This method can result in a least significant digit "jitter" of .01 sec |
  9.  |  on the lap timer.  A better, but more complex method is to use a single |
  10.  |  timer to keep track of both.  The dual timer method makes for better    |
  11.  |  demo code and was chosen for that reason only.                          |
  12.  |                                                                          |
  13.  |  NOTE: Compile with stack checking disabled.                             |
  14.  |                                                                          |
  15.  |  (c)1989 Ryle Design, P.O. Box 22, Mt. Pleasant, Michigan 48804          |
  16.  |                                                                          |
  17.  |  V3.00  Turbo C Shareware Evaluation Series                              |                                    |
  18.  ---------------------------------------------------------------------------*/
  19.  
  20. #include <stdio.h>
  21. #include <conio.h>
  22. #include <dos.h>
  23.  
  24. #include "pchrt.h"
  25.  
  26. #ifndef TRUE
  27.     #define TRUE    1
  28.     #define FALSE   0
  29. #endif
  30.  
  31. #define TIMEROFF    0
  32. #define TIMERON     1
  33. #define LAPSTOP     0
  34. #define LAPRUN      1
  35. #define F1          59
  36. #define F2          60
  37. #define KEYPORT     0x60
  38.  
  39. #define MSMINS      60000000L;
  40. #define MSSECS      1000000L;
  41. #define MSHUNDS     10000L;
  42.  
  43. char            tstring[9] = {"00:00.00"};
  44. char            lstring[9] = {"00:00.00"};
  45.  
  46. int             tstate = TIMEROFF;
  47. int             lstate = LAPSTOP;
  48. int             laps;
  49. unsigned long   ltime = 0;
  50. unsigned long   ttime = 0;
  51.  
  52. pchrt_type      dtime[2];
  53.  
  54. void interrupt  (*old_keybd_int)(void);             /* pointer to old keyboard interrupt */
  55. void interrupt  new_keybd_int(void);                /* our new keyboard interrupt        */
  56.  
  57.  
  58. int hide_cursor(void)
  59. /*--------------------------------------------------------------------------
  60.  |  This function disables the cursor.                                     |
  61.  |                                                                         |
  62.  |  Globals referenced: none                                               |
  63.  |                                                                         |
  64.  |  Arguments : void                                                       |
  65.  |                                                                         |
  66.  |  Returns   : (int) cursor shape for later restoration                   |
  67.  --------------------------------------------------------------------------*/
  68. {
  69.     union REGS  regs;
  70.     int         cursortype;
  71.  
  72.     regs.h.ah = 15;                                         /* get current video page */
  73.     int86(0x10,®s,®s);
  74.  
  75.     regs.h.ah = 3;                                          /* request cursor shape */
  76.     int86(0x10,®s,®s);                                /* regs.bh has video page from last int86() call */
  77.  
  78.     cursortype = regs.h.cl + ( (int) regs.h.ch << 8);       /* store cursor start & stop rasters */
  79.  
  80.     regs.h.ah = 1;                                          /* set cursor shape */
  81.     regs.h.ch = 32;                                         /* set bit 5 - turns cursor off */
  82.     int86(0x10,®s,®s);                                /* and disable cursor */
  83.  
  84.     return(cursortype);
  85.  
  86. } /* hide_cursor */
  87.  
  88.  
  89.  
  90. void set_cursor(int cursortype)
  91. /*---------------------------------------------------------------------------
  92.  |  This function sets the cursor to a new shape.                           |
  93.  |                                                                          |
  94.  |  Globals referenced: none                                                |
  95.  |                                                                          |
  96.  |  Arguments : (int) cursortype - high 8 bits is start raster              |
  97.  |                                 low 8 bits is stop raster                |
  98.  |  Returns   : void                                                        |
  99.  ---------------------------------------------------------------------------*/
  100. {
  101.     union REGS  regs;
  102.  
  103.     regs.h.ah = 1;                                          /* set cursor shape */
  104.     regs.h.ch = (cursortype & 0xFF00) >> 8;                 /* cursor start raster */
  105.     regs.h.cl = (cursortype & 0x00FF);                      /* cursor stop raster */
  106.     int86(0x10,®s,®s);                                /* call BIOS interupt 10h */
  107.  
  108. } /* set_cursor */
  109.  
  110.  
  111.  
  112. void restore_old_keybd_int(void)
  113. /*---------------------------------------------------------------------------
  114.  |  This function restores the original keyboard interrupt and must be      |
  115.  |  called prior to program completion.                                     |
  116.  |                                                                          |
  117.  |  Globals referenced: old_keybd_int                                       |
  118.  |                                                                          |
  119.  |  Arguments: void                                                         |
  120.  |                                                                          |
  121.  |  Returns  : void                                                         |
  122.  ---------------------------------------------------------------------------*/
  123. {
  124.     setvect(9,old_keybd_int);                       /* restore old ISR vector */
  125.  
  126. } /* restore_old_keybd_int */
  127.  
  128.  
  129.  
  130. void install_new_keybd_int(void)
  131. /*---------------------------------------------------------------------------
  132.  |  This function saves the original keyboard interrupt vector and installs |
  133.  |  the address of our user written interrupt handler in the ISR vector     |
  134.  |  table.                                                                  |
  135.  |                                                                          |
  136.  |  Globals referenced: old_keybd_int                                       |
  137.  |                      new_keybd_int                                       |
  138.  |                                                                          |
  139.  |  Arguments: void                                                         |
  140.  |                                                                          |
  141.  |  Returns  : void                                                         |
  142.  ---------------------------------------------------------------------------*/
  143. {
  144.     old_keybd_int = getvect(9);                     /* save old ISR vector    */
  145.     setvect(9,new_keybd_int);                       /* install new ISR vector */
  146.  
  147. } /* install_new_keybd_int */
  148.  
  149.  
  150.  
  151. void interrupt far new_keybd_int(void)
  152. /*---------------------------------------------------------------------------
  153.  |  This function is our new interrupt service routine for interrupt 9h.    |
  154.  |  The following occurs:                                                   |
  155.  |      1. The keyboard hardware port is read to see what key was pressed.  |
  156.  |      2. If F1 or F2 were pressed, the watch state is checked and         |
  157.  |         appropriate action is taken.                                     |
  158.  |      3. The old keyboard interrupt is called.                            |
  159.  |                                                                          |
  160.  |  Since this function is invoked by a hardware interrupt, it functions    |
  161.  |  asynchronously to the main program execution and provides extremely     |
  162.  |  high timing accuracy.                                                   |
  163.  |                                                                          |
  164.  |  Globals referenced: tstate